package com.it00zyq.admin.service.impl;

import com.it00zyq.admin.service.MailService;
import com.it00zyq.common.config.MailProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @Author zyq
 * @Date 2022/5/3 11:02
 * @Description TODO
 * @Version 1.0
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class MailServiceImpl implements MailService {

    private final JavaMailSender sender;
    private final MailProperties properties;

    @Override
    public void sendMail(String subject, String content, String to) {
        try {
            SimpleMailMessage msg = new SimpleMailMessage();
            // 邮件发信人,可从配置文件获取
            msg.setFrom(properties.getFrom());
            // 邮件收信人
            msg.setTo(to);
            // 邮件主题
            msg.setSubject(subject);
            // 邮件内容
            msg.setText(content);
            // 发送邮件
            sender.send(msg);
        } catch(Exception e){
            log.error("[邮件发送失败]: {}", e.getMessage());
        }
    }
}
